home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9511 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  7.3 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.ada,comp.lang.c++,comp.lang.c,comp.lang.modula3,comp.lang.modula2
  4. Subject: Re: Hungarian notation - whoops!
  5. Date: 1 Mar 1996 14:59:25 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4h7vgdINNmsh@anvil.ugrad.cs.ubc.ca>
  8. References: <30C40F77.53B5@swsbbs.com> <4g9255$74s@goanna.cs.rmit.EDU.AU> <4gip1iINNjd@keats.ugrad.cs.ubc.ca> <4h6hlo$hqu@goanna.cs.rmit.EDU.AU>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4h6hlo$hqu@goanna.cs.rmit.EDU.AU>,
  12. Richard A. O'Keefe <ok@goanna.cs.rmit.EDU.AU> wrote:
  13.  >> >I wrote:
  14.  >> >    But the fact that abs(x) may deliver a negative
  15.  >> >    number is something I have to live with the whole time.
  16.  >
  17.  >c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
  18.  >>It does so for the largest negative value. This is documented. Unfortunately,
  19.  >>as you claim, you do have to check that yourself. And of course, to know what
  20.  >>the largest negative value is, you can use the standard #defined manifest
  21.  >>constants. It's not like abs() returns a negative value for any old random
  22.  >>input. The fact of the matter is that the additive inverse of the largest
  23.  >>negative value under two's complement simply can't be represented in a two's
  24.  >>complement word of the same size, yet the return value from abs() is a signed
  25.  >>quantity. Shrug.
  26.  >
  27.  >"shrug"?
  28.  >Every program I write either (a) gets significantly more complex, making it
  29.  >more costly to write, inspect, and debug, or (b) incurs risk of error, and
  30.  >you say "shrug"?
  31.  >
  32.  >Why should I read anything else you write, if our attitudes are so different?
  33.  >
  34.  >As for the rest, which I did read, Kazimir keeps on trying to instruct me
  35.  >about C.  But I ****know**** about C.  I know a LOT about C.  I don't need
  36.  >to be instructed about it.  My problem isn't ignorance, it's dislike.
  37.  >
  38.  >>For _one_ legal input. :)
  39.  >
  40.  >One legal input is one LEGAL input.
  41.  
  42. Whether or not it is a legal input is implementation defined. 
  43.  
  44. What is the _guaranteed_ lowest signed integer, LONG_MIN?
  45.  
  46. From K&R2, page 257:
  47.  
  48.     LONG_MIN    -2143483647    minimum value of long
  49.  
  50. This is clearly not the most negative two's complement 32-bit integer.
  51.  
  52. By feeding -214348364_8_ to the abs() function or the unary - operator, you are
  53. exceeding the stated limit. Thus your code is not strictly compliant with the
  54. standard. 
  55.  
  56. Look, two's complement gives you one extra negative number that you aren't
  57. supposed to use.  A sign/magnitude representation gives you two zeros instead.
  58.  
  59.  
  60.  >I am concerned about writing reliable maintainable software at affordable
  61.  >cost.  I am only interested in hardware, languages, compilers, and so on
  62.  
  63. Then don't expect variables to hold values outside of what is stated
  64. in the standard. 
  65.  
  66.  >as a means to that end.  I am NOT interested in wasting my time fighting
  67.  >around rough edges that do not serve MY ends.
  68.  >
  69.  >>I didn't say that it wasn't. But the Ada compiler has to also do a little
  70.  >>``code explosion'' to ensure the same portability as your C with programmer
  71.  >>inserted masking.
  72.  >
  73.  >Yes, but the COMPILER does it!  _I_ don't have to design, code, inspect, test,
  74.  >debug, document, maintain, &c the code that the compiler generates.  I don't
  75.  >_care_ what the compiler does.  What I care about is that my source code is
  76.  >as clean and straightforward as possible.  Surely I cannot be alone in this,
  77.  >or Ada would not exist.
  78.  
  79. Ada is a fine language. C is a fine language. They exist because of creative
  80. individuals, who have different approaches to a similar problem.
  81.  
  82.  >> >Overflows aren't the problem.  Restricted machine arithmetic is the problem.
  83.  >
  84.  >>Not much you can do about the machine, as a programmer and  supporting
  85.  >>multi-precision arithmetic in any language imposes a lot of overhead and code
  86.  >>explosion.
  87.  >
  88.  >(a) I didn't ask for multi-precision arithmetic.
  89.  >    I meant "restricted" in an entirely different sense.
  90.  >(b) It is simply false that supporting multi-precision arithmetic imposes
  91.  >    a LOT of overhead or code explosion.  Bad implementations may have
  92.  >    such overheads, but it is bad implementation that imposes them, not
  93.  >    high precision arithmetic.  If you mean that supporting numbers up to
  94.  >    18 decimal digits (as required by the COBOL standard) on a 32-bit-only
  95.  >    machine is slower than using confining yourself to what the machine can
  96.  >    do in a single cycle, then yes, but so what?  What counts is overhead
  97.  >    *relative to other ways of getting RIGHT ANSWERS*.  (Note:  on a number
  98.  >    of modern machines a subroutine call is a single instruction, so if we
  99.  >    compare
  100.  >    load r1, X
  101.  >    load r2, Y
  102.  >    add r1, r1, r2
  103.  >    store r1, Z
  104.  >    with
  105.  >    load r1, @X
  106.  >    load r2, @Y
  107.  >    call,delayed add_64
  108.  >    load r3, @Z
  109.  >    we find no difference whatsoever in the code size.  Yes, it's slower,
  110.  >    but it gets the right answer.
  111.  >
  112.  >
  113.  >Look, let's drop this.  As far as I was concerned, the real issue is
  114.  >that
  115.  >
  116.  >    hardware and software exist to help us solve real-world problems
  117.  >    they should be co-designed to minimise the cost of reliable
  118.  >    correct computing.
  119.  
  120.  >There is no need to educate me about C or present CPUs.  C was, and in its
  121.  >essence still is, a language which is designed to make it easy for an
  122.  >expert programmer to control a machine.  There is certainly room for such
  123.  >languages, even assembly code has its niche.  My complaint is not about
  124.  >2s-complement as such, it is about programming languages that let it show
  125.  >through in the form of giving wrong answers.  It is NOT my job to make my
  126.  
  127. Hiding the two's complement representation would require the generation of all
  128. kinds of extra code. It would be to the detriment of programs that never go
  129. near the ``largest negative value''. 
  130.  
  131.  >I already explained my gripe in detail.  The "extra" value may simplify
  132.  >life for computer architects.  But it _doesn't_ simplify life for
  133.  >programmers.  Example:
  134.  >
  135.  >    int like_atoi(char *p) {
  136.  >        int sign = 1;
  137.  >        int n = 0;
  138.  >
  139.  >        if (*p == '+') p++; else    
  140.  >        if (*p == '-') p++, sign = -1;
  141.  >
  142.  >        while (isdigit(*p)) n = n*10 + (*p++ - '0');
  143.  >        return n*sign;
  144.  >    }
  145.  >    
  146.  >If you are on a machine where signed arithmetic overflows are signalled
  147.  >(MIPS), why does this have to be written
  148.  
  149. It doesn't have to be written with reference to what machine you are using.
  150.  
  151.  >    int like_atoi(char *p) {
  152.  >        int sign = -1;
  153.  >        int n = 0;
  154.  >
  155.  >        if (*p == '+') p++; else
  156.  >        if (*p == '-') p++, sign = 1;
  157.  >
  158.  >        while (isdigit(*p)) n = n*10 - (*p++ - '0');
  159.  >        return n*sign;
  160.  >    }
  161.  
  162. This program is poor, because it doesn't place restrictions on the input. If I
  163. feed it the string '123412341452345254623542435', it will produce a valid
  164. variable of type 'int', without signalling that an invalid value has been
  165. input.  Two's complement is the least of your problems here.
  166.  
  167. By the way, ints can have the range as low -32767 to 32767, which further
  168. restricts the robustness of the code.
  169.  
  170. If you write it properly, you could come up with a portable routine which works
  171. for decimal representations of integers in the range LONG_MIN and LONG_MAX, and
  172. signals an error condition for others. (I mean the *Standard's* minimum values
  173. for LONG_MIN and LONG_MAX, of course, not implementation-defined values derived
  174. from a local <limits.h>).
  175.  
  176. Is this your prime evidence that C is bad or do you have more?
  177. -- 
  178.  
  179.